﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Routing;
using VQWEB.administrator.sources;
using System.Web.Script.Serialization;
using VQWEB.common;
using VQData;
namespace VQWEB.administrator.handlers
{
    /// <summary>
    /// Summary description for TestHandler
    /// </summary>
    public class eng_TestHandler : IHttpHandler,IRouteHandler
    {
        
        JavaScriptSerializer serializer = new JavaScriptSerializer();
        public void ProcessRequest(HttpContext context)
        {
            #region Test     
            string add_test = context.Request["add_test"];
            if (!string.IsNullOrEmpty(add_test))
            {
                BusinessLayer<EnglishModelDataContext> bussiness = new BusinessLayer<EnglishModelDataContext>();
                var obj = bussiness.InsertOrUpdate<eng_test>(add_test);
                context.Response.Write("Success Full");
            }
            string view_test = context.Request["view_test"];
            if (!string.IsNullOrEmpty(view_test))
            {
                string Id = context.Request["Id"];
                RenderSingleTest(context, Convert.ToInt32(Id));
            }
            string list_test = context.Request["list_test"];
            if(!string.IsNullOrEmpty(list_test)){
                RenderTest(context);
            }
            string delete_test = context.Request["delete_test"];
            if (!string.IsNullOrEmpty(delete_test)) {
                BusinessLayer<EnglishModelDataContext> bussiness = new BusinessLayer<EnglishModelDataContext>();
                bussiness.Delete<eng_test>(delete_test);
                RenderTest(context);
            }
            string lockunlock_test = context.Request["lockunlock_test"];
            if (!string.IsNullOrEmpty(lockunlock_test)) {
                BusinessLayer<EnglishModelDataContext> bussiness = new BusinessLayer<EnglishModelDataContext>();
                bussiness.LockUnLock<eng_test>(lockunlock_test, "test_active");
                RenderTest(context);
            }
            #endregion

            #region Test Part
            string add_test_part = context.Request["add_test_part"];
            if (!string.IsNullOrEmpty(add_test_part))
            {
                BusinessLayer<EnglishModelDataContext> bussiness = new BusinessLayer<EnglishModelDataContext>();
                var obj = bussiness.InsertOrUpdate<eng_tests_part>(add_test_part);
                bussiness.InsertAllInOne<eng_test_part_group>(obj.group_array_id, "group_excercise_id", "part_test_id", obj.part_test_id);
                bussiness.InsertAllInOne<eng_test_excercise>(obj.excercise_array_id, "excercise_id", "part_test_id", obj.part_test_id);
                context.Response.Write("Success Full");
            }
            string view_test_part = context.Request["view_test_part"];
            if (!string.IsNullOrEmpty(view_test_part))
            {
                string Id = context.Request["Id"];
                RenderSingleTestPart(context, Convert.ToInt32(Id));
            }
            string list_test_part = context.Request["list_test_part"];
            if (!string.IsNullOrEmpty(list_test_part))
            {
                RenderTestPart(context);
            }
            string delete_test_part = context.Request["delete_test_part"];
            if (!string.IsNullOrEmpty(delete_test_part))
            {
                BusinessLayer<EnglishModelDataContext> bussiness = new BusinessLayer<EnglishModelDataContext>();
                bussiness.Delete<eng_tests_part>(delete_test_part);
                RenderTestPart(context);
            }
            string lockunlock_test_part = context.Request["lockunlock_test_part"];
            if (!string.IsNullOrEmpty(lockunlock_test_part))
            {
                BusinessLayer<EnglishModelDataContext> bussiness = new BusinessLayer<EnglishModelDataContext>();
                bussiness.LockUnLock<eng_tests_part>(lockunlock_test_part, "part_test_active");
                RenderTestPart(context);
            }
            #endregion
        }
        private void RenderSingleTest(HttpContext context, int Id)
        {
            DataLayer<EnglishModelDataContext> layer = new DataLayer<EnglishModelDataContext>();
            object obj = layer.Get<eng_test>(
               c => c.test_id.Equals(Id),
               c => new
               {
                   id = c.test_id,
                   courseid=c.course_id,
                   typecode = c.type_code,
                   title = c.test_title,
                   content=c.test_content,
                   time=c.test_time,
                   active = c.test_active
               });
            string json = serializer.Serialize(obj);
            context.Request.ContentType = "application/json";
            context.Response.Write(json);
        }
        private void RenderTest(HttpContext context)
        {
            DataLayer<EnglishModelDataContext> layer = new DataLayer<EnglishModelDataContext>();
            IList<object> list = layer.List<eng_test>(
                null,
                null,
                null,
                c => c.test_title,
                c => new
                {
                    id = c.test_id,
                    courseid = c.course_id,
                    typecode = c.type_code,
                    title = c.test_title,
                    content = c.test_content,
                    time = c.test_time,
                    active = c.test_active
                });
            string json = serializer.Serialize(list);
            context.Request.ContentType = "aplication/json";
            context.Response.Write(json);
        }
        private void RenderSingleTestPart(HttpContext context, int Id)
        {
            DataLayer<EnglishModelDataContext> layer = new DataLayer<EnglishModelDataContext>();
            object obj = layer.Get<eng_tests_part>(
               c => c.part_test_id.Equals(Id),
               c => new
               {
                   id = c.part_test_id,
                   title = c.part_test_title,
                   content=c.part_test_content,
                   file = c.part_test_file,
                   active = c.part_test_active
               });
            string json = serializer.Serialize(obj);
            context.Request.ContentType = "application/json";
            context.Response.Write(json);
        }
        private void RenderTestPart(HttpContext context)
        {
            DataLayer<EnglishModelDataContext> layer = new DataLayer<EnglishModelDataContext>();
            IList<object> list = layer.List<eng_tests_part>(
                null,
                null,
                null,
                c => c.part_test_title,
                 c => new
                 {
                     id = c.part_test_id,
                     title = c.part_test_title,
                     content = c.part_test_content,
                     file = c.part_test_file,
                     active = c.part_test_active
                 });
            string json = serializer.Serialize(list);
            context.Request.ContentType = "aplication/json";
            context.Response.Write(json);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

        public IHttpHandler GetHttpHandler(RequestContext requestContext)
        {
            return this;
        }
    }
}